package com.cloudm.technician.common.login

import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.action.ViewActions.click
import android.support.test.espresso.action.ViewActions.typeText
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import com.cloudm.technician.BaseMockServer
import com.cloudm.technician.R
import com.cloudm.technician.data.model.Response
import com.cloudm.technician.data.model.User
import com.cloudm.technician.ui.modules.common.login.LoginActivity
import org.hamcrest.core.IsNot.not
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
 * 作者: wesley
 * 时间: 2017/12/19
 * 描述: 登录界面view层的单元测试
 */
@RunWith(AndroidJUnit4::class)
class LoginScreenTest : BaseMockServer() {

    @JvmField
    @Rule
    val rule = ActivityTestRule<LoginActivity>(LoginActivity::class.java, true, true)

    private val phoneNumber = "18255215889"
    private val password = "111111"

    @Test
    fun testIvClearVisibleOne() {
        onView(withId(R.id.etPhoneNumber)).perform(typeText(phoneNumber))
        onView(withId(R.id.llClearPhoneNumber)).check(matches(isDisplayed()))
    }

    @Test
    fun testIvClearVisibleTwo() {
        onView(withId(R.id.etPhoneNumber)).perform(typeText(""))
        onView(withId(R.id.llClearPhoneNumber)).check(matches(not(isDisplayed())))
    }


    @Test
    fun testIvClearVisibleFour() {
        onView(withId(R.id.etPhoneNumber)).perform(typeText("   "))
        onView(withId(R.id.llClearPhoneNumber)).check(matches(not(isDisplayed())))
    }

    @Test
    fun testIvClearVisibleFive() {
        onView(withId(R.id.etPhoneNumber)).perform(typeText(phoneNumber))
        onView(withId(R.id.etPassword)).perform(typeText(password))
        onView(withId(R.id.llClearPhoneNumber)).check(matches(not(isDisplayed())))
    }

    @Test
    fun testIvClearVisibleSix() {
        onView(withId(R.id.etPassword)).perform(typeText(password))
        onView(withId(R.id.etPhoneNumber)).perform(typeText(phoneNumber))
        onView(withId(R.id.llClearPhoneNumber)).check(matches(isDisplayed()))
    }


    @Test
    fun testBtnLoginEnabledOne() {
        onView(withId(R.id.etPhoneNumber)).perform(typeText(phoneNumber))
        onView(withId(R.id.etPassword)).perform(typeText(password))
        onView(withId(R.id.btnLogin)).check(matches(isEnabled()))
    }

    @Test
    fun testBtnLoginEnabledTwo() {
        onView(withId(R.id.etPhoneNumber)).perform(typeText(""))
        onView(withId(R.id.etPassword)).perform(typeText(password))
        onView(withId(R.id.btnLogin)).check(matches(not(isEnabled())))
    }

    @Test
    fun testBtnLoginEnabledFour() {
        onView(withId(R.id.etPhoneNumber)).perform(typeText("   "))
        onView(withId(R.id.etPassword)).perform(typeText(password))
        onView(withId(R.id.btnLogin)).check(matches(not(isEnabled())))
    }


    @Test
    fun testBtnLoginEnabledFive() {
        onView(withId(R.id.etPhoneNumber)).perform(typeText(phoneNumber))
        onView(withId(R.id.etPassword)).perform(typeText(""))
        onView(withId(R.id.btnLogin)).check(matches(not(isEnabled())))
    }


    @Test
    fun testBtnLoginEnabledEight() {
        onView(withId(R.id.etPhoneNumber)).perform(typeText(phoneNumber))
        onView(withId(R.id.etPassword)).perform(typeText("   "))
        onView(withId(R.id.btnLogin)).check(matches(isEnabled()))
    }


    @Test
    fun testLlClearPhoneNumberClick() {
        onView(withId(R.id.etPhoneNumber)).perform(typeText(phoneNumber))
        onView(withId(R.id.llClearPhoneNumber)).perform(click())
        onView(withId(R.id.llClearPhoneNumber)).check(matches(not(isDisplayed())))
        onView(withId(R.id.etPhoneNumber)).check(matches(withText("")))
    }


    @Test
    fun testForgePasswordClick() {
        onView(withId(R.id.tvForgePassword)).perform(click())
        onView(withId(R.id.tvHeaderTitle)).check(
                matches(withText(rule.activity.resources.getString(R.string.find_password))))
    }

    @Test
    fun testRegisterClick(){
        onView(withId(R.id.menu_next)).perform(click())
        onView(withId(R.id.tvHeaderTitle)).check(
                matches(withText(rule.activity.resources.getString(R.string.register))))
    }

    @Test
    fun testLoginOne() {
        val user = User()
        val response = Response(800, "", user, null)
        mockWebData(response)
        onView(withId(R.id.etPhoneNumber)).perform(typeText(phoneNumber))
        onView(withId(R.id.etPassword)).perform(typeText(password))
        onView(withId(R.id.btnLogin)).perform(click())
        onView(withId(R.id.tvHeaderTitle)).check(
                matches(withText(rule.activity.resources.getString(R.string.home))))

    }

    @Test
    fun testLoginTwo() {
        val user = User()
        val response = Response(80, "", user, null)
        mockWebData(response)
        onView(withId(R.id.etPhoneNumber)).perform(typeText(phoneNumber))
        onView(withId(R.id.etPassword)).perform(typeText(password))
        onView(withId(R.id.btnLogin)).perform(click())
        onView(withId(R.id.btnLogin)).check(matches(isDisplayed()))
    }
}